<template>
  <el-dialog
    v-loading="loading"
    :visible.sync="visible"
    :append-to-body="true"
    :close-on-click-modal="false"
    title="批量转移"
    width="450px"
    @close="handleCancel"
  >
    <div class="handle-box">
      <flexbox class="handle-item">
        <div class="handle-item-name">
          {{ transferTit() }}
        </div>
        <user-select
          class="handle-item-content"
          v-model="usersList"
          @input="userChange"
        />
      </flexbox>
    </div>
    <span slot="footer" class="dialog-footer">
      <el-button @click.native="handleCancel">取消</el-button>
      <el-button v-debounce="handleConfirm" type="primary">保存</el-button>
    </span>
  </el-dialog>
</template>

<script>
import UserSelect from "@/views/admin/components/UserSelect"; // 人员选择器
import { crmContractTransferTeacherAPI } from "@/api/crm/contract";
import { crmReceivablesTransferTeacherAPI } from "@/api/crm/receivables";
import { crmRefundTransferTeacherAPI } from "@/api/crm/refund";
import { mapGetters } from "vuex";
export default {
  /** 学员管理 的 勾选后的 转移讲师 操作*/
  name: "transferTeacher",
  components: {
    UserSelect
  },
  mixins: [],
  props: {
    dialogVisible: {
      type: Boolean,
      required: true,
      default: false
    },
    /** 没有值就是全部类型 有值就是当个类型 */
    crmType: {
      type: String,
      default: ""
    },
    /** 转移数据 */
    selectionList: {
      type: Array,
      default: () => {
        return [];
      }
    }
  },
  data() {
    return {
      loading: false, // 加载动画
      visible: false,

      usersList: "", // 变更负责人
      addsTypes: [], // 添加至
      leadsUserId: "" // 变更线索负责人
    };
  },
  computed: {
    ...mapGetters(["crm"]),
  },
  watch: {
    dialogVisible: {
      handler(val) {
        this.visible = val;
        if (!val) {
          this.resetData();
        }
      },
      deep: true,
      immediate: true
    }
  },
  mounted() {
    this.visible = this.dialogVisible;
  },
  methods: {
    transferTit() {
      return "变更讲师负责人为";
    },
    /**
     * 重置信息
     */
    resetData() {
      this.usersList = "";
      this.leadsUserId = "";
      this.addsTypes = []; // 添加至
    },

    /**
     * 取消选择
     */
    handleCancel() {
      this.resetData();
      this.visible = false;
      this.$emit("update:dialogVisible", false);
    },
    /** 讲师更改 */
    userChange(data) {
      this.usersList = data;
    },
    handleConfirm() {
      if (!this.usersList) {
        this.$message.error("请选择变更讲师负责人");
      } else {
        this.handleStepTwo();
      }
    },
    handleStepTwo() {
      this.loading = true;
      this.getRequest()(this.getParams())
        .then(res => {
          this.$message({
            type: "success",
            message: "操作成功"
          });
          this.loading = false;
          this.handleCancel();
          this.$emit("handle", { type: "transfer" });
          // 刷新待办
          this.$store.dispatch("GetMessageNum");
        })
        .catch(() => {
          this.loading = false;
        });
    },
    // 异步请求接口
    getRequest() {
      if (this.crmType === "contract") {
        return crmContractTransferTeacherAPI;
      } else if (this.crmType === "receivables") {
        return crmReceivablesTransferTeacherAPI;
      } else if (this.crmType === "refund") {
        return crmRefundTransferTeacherAPI;
      }
    },
    // 参数
    getParams() {
      var ownerUserId = this.usersList;
      var params = {
        teacherId: ownerUserId
      };
      params["ids"] = this.selectionList.map(item =>
        parseInt(item[this.crmType + "Id"])
      );
      return params;
    }
  }
};
</script>

<style lang="scss" scoped>
.handle-box {
  color: #333;
  font-size: 12px;
}
.handle-item {
  padding-bottom: 15px;
  .handle-item-name {
    flex-shrink: 0;
    width: 115px;
  }
  .handle-item-content {
    flex: 1;
  }
}
.handle-bar {
  position: relative;
  margin-top: 10px;
  height: 30px;
  button {
    float: right;
    margin-right: 10px;
  }
}
</style>
